<?php

/**
 * This is the model class for table "attribute".
 *
 * The followings are the available columns in table 'attribute':
 * @property integer $id_attribute
 * @property string $name
 * @property integer $id_matter
 * @property integer $id_analysis
 * @property string $values
 * @property string $note
 * @property string $date_add
 * @property string $date_upd
 * @property integer $del_flg
 */
class Attribute extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Attribute the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'attribute';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('id_matter, id_analysis, name', 'required'),
			array('id_matter, del_flg', 'numerical', 'integerOnly'=>true),
			array('name, values, note', 'length', 'max'=>255),
			array('date_add, date_upd', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id_attribute, name, id_matter, id_analysis, values, note, date_add, date_upd, del_flg', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
            'mt' => array(self::BELONGS_TO, 'Matter', 'id_matter'),
            'al' => array(self::BELONGS_TO, 'Analysis', 'id_analysis'),
            'task' => array(self::HAS_MANY, 'Task', 'id_attribute'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id_attribute' => '#',
			'name' => Yii::t('app' ,'Name'),
			'id_matter' => Yii::t('app' ,'Matter'),
			'id_analysis' => Yii::t('app' ,'Analysis'),
			'values' => Yii::t('app' ,'Values'),
			'note' => Yii::t('app' ,'Note'),
			'date_add' => Yii::t('app' ,'Date Add'),
			'date_upd' => Yii::t('app' ,'Date Update'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

        $criteria->addCondition('del_flg = 0');
		$criteria->compare('id_attribute',$this->id_attribute);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('id_matter',$this->id_matter, true);
		$criteria->compare('id_analysis',$this->id_analysis, true);
		$criteria->compare('values',$this->values,true);
		$criteria->compare('note',$this->note,true);

		return new CActiveDataProvider($this, array(
            'pagination'=>array(
                'pageSize'=>SystemConfig::PAGE_SIZE,
            ),
			'criteria'=>$criteria,
		));
	}

    public function beforeSave() {
        $now = new CDbExpression('NOW()');
        if ($this->isNewRecord) $this->date_add = $now;

        $this->date_upd = $now;

        return parent::beforeSave();
    }

    /**
     * get all attribute matter to array
     * @return array
     */
    public static function getListAttribute(){
        // get list attribute matter to select
        $mAttribute = Attribute::model()->findAll('del_flg = 0');
        $listAttribute = array();
        if ($mAttribute) {
            foreach ($mAttribute as $value) {
                $listAttribute[$value->id_attribute] = $value->name;
            }
        }

        return $listAttribute;
    }

    public static function getAttributeById($id){
        $criteria=new CDbCriteria;
        $criteria->addCondition('del_flg = 0');
        $mAttribute = Attribute::model()->findByPk($id, $criteria);
        if($mAttribute){
            return $mAttribute;
        }else{
            return false;
        }
    }
}